package com.utils;// src/main/java/com/yourpackage/utils/EmailUtils.java (或者你存放工具类的包)

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; // <--- 添加这个注解

/**
 * 邮箱工具类
 * 主要用来注册时发送验证码
 * */
@Component
public class EmailUtil {
    private final JavaMailSender mailSender;

    @Autowired
    public EmailUtil(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    public static final long VERIFICATION_CODE_VALID_MINUTES = 5;

    @Async
    public void sendVerificationCodeAsync(String toEmail, String code) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("17786408269@163.com");
            message.setTo(toEmail);
            message.setSubject("您的注册验证码");
            message.setText("您的注册验证码是：" + code + "。请在" + VERIFICATION_CODE_VALID_MINUTES + "分钟内完成验证。");
            mailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
